﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-24>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：来涵表信息
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Web.Controls.LAIHAN;

namespace Web.Controls
{
    public partial class UCLHBiaoInfoQS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX
        {
            get;
            set;
        }

        /// <summary>
        /// 当前流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get;
            set;
        }

        /// <summary>
        /// 井编号
        /// </summary>
        public string JINBH
        {
            set
            {
                this.txtJinBH.Text = value;
            }
            get
            {
                return this.txtJinBH.Text;
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string YONGHUM
        {
            set
            {
                this.txtYongHuM.Text = value;
            }
            get
            {
                return this.txtYongHuM.Text;
            }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string DIZHI
        {
            set
            {
                this.txtDiZhi.Text = value;
            }
            get
            {
                return this.txtDiZhi.Text;
            }
        }

        /// <summary>
        /// 来函编号
        /// </summary>
        public string LAIHANBH
        {
            set
            {
                this.txtLaiHanBH.Text = value;
            }
            get
            {
                return this.txtLaiHanBH.Text;
            }
        }

       

        /// <summary>
        /// 判断引用页面类别   1月调整登记  2月调整修改  3其他页面调用 不可修改
        /// </summary>
        public int ControlType
        {
            get { return Convert.ToInt32(ViewState["ControlType"]); }
            set { ViewState["ControlType"] = value; }
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        private string errorMsg
        {
            get
            {
                return Convert.ToString(ViewState["errorMsg"]);
            }
            set
            {
                ViewState["errorMsg"] = value;
            }
        }

        #endregion

        #region 变量

        #endregion

        #region 事件

        /// <summary>
        /// 加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 回调响应
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "bh")//响应编号回车事件
            {
                DataRowCollection drc = new SpHelper().P_QS_BIAOXXHQ((int)this.LaiHanLX,null,BusinessTool.WType,this.txtShuiBiaoBH.Text).Tables[0].Rows;
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = false;
                    cr.Msg = "查询不到该表信息。";
                    cr.IsAlertMsg=true;
                    e.Result=cr.ToString();
                    return;
                }
                else
                {
                    if(this.LaiHanLX==eLaiHanLX.水资源销表)
                    {
                        //销表验证
                        int? rsno = -1;
                        string rsmsg = "";
                        if (BusinessTool.WType == 2)
                        {
                            new SpHelper().P_QSLH_XIAOBIAOYZ_DXS(this.txtShuiBiaoBH.Text, out rsno, out rsmsg);
                            if (rsno <= 0)
                            {
                                cr.Msg = rsmsg + ".";
                                cr.IsSuccess = false;
                                cr.IsAlertMsg = true;
                                e.Result = cr.ToString();
                                return;
                            }
                        }
                        //添加验证
                        UCLHShuiBiaoListQS sbList = this.Parent.FindControl("UCLHShuiBiaoListQS") as UCLHShuiBiaoListQS;
                        ValidateRS rsT = sbList.ValidateAdd(this.txtShuiBiaoBH.Text, Convert.ToString(drc[0]["S_YONGHUH"]));
                        if(!rsT.IsSuccess)
                        {
                            cr.Msg = rsT.Msg + "!";
                            cr.IsSuccess = false;
                            cr.IsAlertMsg = true;                            
                            e.Result = cr.ToString();
                            return;
                        }
                    }
                    this.errorMsg = "";
                    cr.AddValue(Convert.ToString(drc[0]["S_BIAOHAO"]), Convert.ToString(drc[0]["S_KOUJING"]), Convert.ToString(drc[0]["S_YONGHUH"]), Convert.ToString(drc[0]["S_YONGHUM"]), Convert.ToString(drc[0]["S_JINGBH"]), Convert.ToString(drc[0]["S_LAOBIANH"]), Convert.ToString(drc[0]["S_JINGDIZ"]));
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
            else if (e.Parameter == "bh2")//响应目标用户号回车事件
            {
                DataRowCollection drc = new QS_YG_YONGHUXXBLL().SelectQS_YG_YONGHUXXesDynamic("I_JLZT=0 and I_WTYPE=" + BusinessTool.WType + " and S_YONGHUH='" + this.txtTargetYongHuH.Text + "'", null).Rows;
                if (drc.Count == 0)
                {
                    cr.IsSuccess = false;
                    cr.IsAlertMsg = false;
                    cr.Msg = "无此用户。";
                    cr.IsAlertMsg = true;
                    e.Result = cr.ToString();
                    return;
                }
                else
                {
                    cr.AddValue(Convert.ToString(drc[0]["S_YONGHUM"]));
                    cr.IsAlertMsg = false;
                    cr.IsSuccess = true;
                    e.Result = cr.ToString();
                }
            }
        }

        /// <summary>
        /// 前台验证后实际执行后台操作
        /// </summary>
        protected void btnActPerform_Click(object sender, EventArgs e)
        {
            YS_LH_LAIHANXX laihanxx = new YS_LH_LAIHANXX();
            laihanxx.S_LAIHANBH = null;
            laihanxx.I_LAIHANLX = (int)this.LaiHanLX;
            laihanxx.I_LIUCHENGBH = (int)eLaiHanLCBH.未登记;
            laihanxx.S_YONGHUBH = this.txtYongHuH.Text;
            laihanxx.S_BEIZHU = this.txtShuiBiaoBH.Text;
            UCLHShuiBiaoListQS ucinfo = null;      
            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                ucinfo = this.Parent.FindControl("UCLHShuiBiaoListQS") as UCLHShuiBiaoListQS;
                ucinfo.RegistMemberValue(this.txtYongHuH.Text, null, this.txtShuiBiaoBH.Text);
                ucinfo.Add(this.txtShuiBiaoBH.Text,this.txtJinBH.Text,this.txtYongHuH.Text);                
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源表过户)
            {
                ucinfo = this.Parent.FindControl("UCLHShuiBiaoListQS") as UCLHShuiBiaoListQS;
                if (ucinfo != null)
                {
                    ((UCLHShuiBiaoListQS)ucinfo).RegistMemberValue(new string[] { this.txtYongHuH.Text, this.txtTargetYongHuH.Text,this.txtShuiBiaoBH.Text});
                    ucinfo.LoadInfo(laihanxx);
                }
            }            
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            //默认设置错误信息,防止用户不搜索直接提交
            this.errorMsg = "error";
            this.txtBiaoHao.ClientEnabled = false;
            this.txtDiZhi.ClientEnabled = false;
            this.txtJinBH.ClientEnabled = false;
            this.txtKouJing.ClientEnabled = false;
            this.txtLaiHanBH.ClientEnabled = false;
            this.txtLaoBH.ClientEnabled = false;
            this.txtXuKeZH.ClientEnabled = false;
            this.txtYongHuDZ.ClientEnabled = false;
            this.txtYongHuH.ClientEnabled = false;
            this.txtYongHuM.ClientEnabled = false;
            this.txtShuiBiaoBH.ClientEnabled = true;
            this.txtTargetYongHuH.ClientEnabled = true;
            this.txtTargetYongHuM.ClientEnabled = false;
            this.trYHDZ.Visible = false;
            this.trTarget1.Visible = (this.LaiHanLX == eLaiHanLX.水资源表过户);
            this.trTarget2.Visible = (this.LaiHanLX == eLaiHanLX.水资源表过户);
            if (BusinessTool.WType == 1)
            {
                this.tJinK.InnerText = "取水口号：";
                this.tYuanJin1.Visible = false;
                this.tYuanJin2.Visible = false;
                this.dxs1.Visible=false;
                this.dxs2.Visible=false;
                this.dxs3.Visible=false;
                this.dxs4.Visible = false;
            }
            else
            {
                this.tJinK.InnerText = "井号：";
                this.tYuanJin1.Visible = true;
                this.tYuanJin2.Visible = true;
            }
            this.txtShuiBiaoBH.Focus();
            
        }

        /// <summary>
        ///根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //根据类型显示需要的             
            //受理之后不可编辑.下方switch中case 3可去除.使用此控件的受理之后的页面该控件属性需加上LaiHanLC="受理"
            if ((int)this.LaiHanLC < (int)eLaiHanLCBH.预登记)
            {
                this.txtShuiBiaoBH.ClientEnabled = true;
                this.txtTargetYongHuH.ClientEnabled = true;
            }
            if ((int)this.LaiHanLC >= (int)eLaiHanLCBH.预登记)
            {
                this.txtShuiBiaoBH.ClientEnabled = false;
                this.txtTargetYongHuH.ClientEnabled = false;
            }
        }

        /// <summary>
        /// 验证数据
        /// </summary>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            if (this.LaiHanLC == eLaiHanLCBH.未登记)
            {
                //判断用户号是否存在
                if (this.txtJinBH.Text == "")
                {
                    rs.Msg = "请输入表编号。";
                    return rs;
                }
                if (this.trTarget1.Visible && (this.txtTargetYongHuH.Text == "" || this.txtTargetYongHuM.Text == ""))
                {
                    rs.Msg = "请将目标用户填写完整。";
                    return rs;
                }
            }       
            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearInfo()
        {
            this.txtDiZhi.Text = "";
            this.txtJinBH.Text = "";
            this.txtLaiHanBH.Text = "";
            this.txtXuKeZH.Text = "";
            this.txtLaoBH.Text = "";
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.txtTargetYongHuH.Text = "";
            this.txtTargetYongHuM.Text = "";
            this.txtShuiBiaoBH.Text = "";
            this.txtBiaoHao.Text = "";
            this.txtKouJing.Text = "";
        }
   

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            //登记前
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                SetDisplay();
                return;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                SetDisplay();
            }            
            //加载许可号等信息
            this.LAIHANBH = laihanxx.S_LAIHANBH;
            this.txtXuKeZH.Text = laihanxx.S_XUKEBH;
            //加载信息
            DataRow dr = new SpHelper().P_QS_BIAOXXHQ((int)this.LaiHanLX, laihanxx.S_LAIHANBH, BusinessTool.WType, null).Tables[0].Rows[0];
            this.txtBiaoHao.Text = Convert.ToString(dr["S_BIAOHAO"]);
            this.txtDiZhi.Text = Convert.ToString(dr["S_JINGDIZ"]);
            this.txtJinBH.Text = Convert.ToString(dr["S_JINGBH"]);
            this.txtKouJing.Text = Convert.ToString(dr["S_KOUJING"]);
            this.txtLaiHanBH.Text = laihanxx.S_LAIHANBH;
            this.txtLaoBH.Text = Convert.ToString(dr["S_LAOBIANH"]);
            this.txtXuKeZH.Text = laihanxx.S_XUKEBH;
            this.txtYongHuH.Text = Convert.ToString(dr["S_YONGHUH"]);
            this.txtYongHuM.Text = Convert.ToString(dr["S_YONGHUM"]);
            this.txtShuiBiaoBH.Text = Convert.ToString(dr["S_SHUIBIAOBH"]);
            this.txtTargetYongHuH.Text = Convert.ToString(dr["S_MUBIAOYHH"]);
            this.txtTargetYongHuM.Text = Convert.ToString(dr["S_MUBIAOYHM"]);
        }
        /*
        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(this.txtYongHuH.Text, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, (BusinessTool.GetWaterYM() / 100).ToString(), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100,out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_M = BusinessTool.GetWaterYM() % 100;
                laihan.S_BEIZHU = this.txtShuiBiaoBH.Text;
                laihan.S_YONGHUBH = this.txtYongHuH.Text;
                laihan.S_YONGHUM = this.txtYongHuM.Text;
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }
        */
        /// <summary>
        /// 获取来函用户信息
        /// </summary>
        /// <param name="laihan"></param>
        /// <returns></returns>
        public YS_LH_YONGHUXX GetLHYONGHUXX(YS_LH_LAIHANXX laihan)
        {            
            return null;
        }

        public void CreateShengQingXX(YS_LH_LAIHANXX lhxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                QS_LH_XIAOBIAOSBXXProcess bllT2 = new QS_LH_XIAOBIAOSBXXProcess();
                bllT2.DeleteQS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + lhxx.S_LAIHANBH);
                //添加销表水表记录
                QS_LH_XIAOBIAOSBXX item = new QS_LH_XIAOBIAOSBXX();
                item.S_LAIHANBH = lhxx.S_LAIHANBH;
                item.S_SHUIBIAOBH = this.txtShuiBiaoBH.Text;
                bllT2.InsertOrUpdate(item);
            }
        }
       
        #endregion

    }
}